/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package csse3004.GD.PM;

import csse3004.GD.DA.PollMasterDA;
import csse3004.GD.Question;
import java.util.ArrayList;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

/**
 *
 * @author David Bullock 42062222
 */
public class adminController implements Controller {

    protected final Log logger = LogFactory.getLog(getClass());
    private PollMasterDA service = new PollMasterDA();

    public ModelAndView handleRequest(HttpServletRequest hsr, HttpServletResponse hsr1) {
        ModelAndView mav = new ModelAndView("admin");
        try {
            int pid = Integer.parseInt(hsr.getParameter("pid"));
            if (hsr.getParameter("next") != null && hsr.getParameter("seconds") != null) {
                int seconds = Integer.parseInt(hsr.getParameter("seconds"));
                if (hsr.getParameter("next").equals("true")) {
                   // service.setNext(true, pid, seconds);
                    logger.info("setting next");
                } else if (hsr.getParameter("next").equals("false")) {
                    //service.setNext(false, pid, seconds);
                    logger.info("setting previous");
                }
            }
            if (hsr.getParameter("object") != null) { //TODO @David FIX THIS
                service.setNewObject(service.getActiveQuestion(pid).getId(), "<img src=\"http://t1.gstatic.com/images?q=tbn:ANd9GcTt7IYhzpvBZbR2csgVYvH8nN9GmlXBOAVAGdPZbwo6ueNybZBW\"/>");
            }

            mav.addObject("set", true);
            mav.addObject("pid", pid);
            return mav;
        } catch (NumberFormatException E) {
            mav.addObject("set", false);
            return mav;
        }
    }

    /**
     * Gets the active question for the specified PollId. It will populate csse3004.GD.Question fully
     * @param pid Poll to populate
     * @return Populated question. Null if there is no active Question set
     */
    public Question getActiveQuestion(int pid) {
        ArrayList<Question> questions = new Question().genTestQuestion(pid);
        Question quest = questions.get(0);
        logger.info(quest.getText());
        return quest;
    }
}
